package com.aftersale.demo.controller;

import com.aftersale.demo.entity.*;
import com.aftersale.demo.mapper.ProblemMapper;
import com.aftersale.demo.service.NoticeService;
import com.aftersale.demo.service.ProblemService;
import com.aftersale.demo.utils.ValidGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.*;
import java.util.List;

/**
*@Author  空
*@Date    2023/10/23 20:24
*@Desc
*/
@RestController
@RequestMapping("/home")
@Api(value="首页模块")
public class HomeController {
    @Autowired
    ProblemService problemService;
    @Autowired
    ProblemMapper problemMapper;
    @Autowired
    NoticeService noticeService;

    @GetMapping("/getopennotice")
    @ApiOperation(value = "公开公告查看接口")
    public Result getOpenNotice(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        return noticeService.getOpenNotice(pageNum,pageSize);
    }

    @GetMapping("/selectallproblem")
    @ApiOperation(value = "售后问题查看接口")
    public Result problems(@RequestParam int roleId,@RequestParam int userId,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize){
        return problemService.problems(userId,roleId,pageNum,pageSize);
    }

    @DbLog(operation = "问题",type = LogType.UPDATE)
    @PutMapping("/updateproblemstate")
    @PreAuthorize("hasAuthority('problem_state_update')")
    @ApiOperation(value = "售后问题状态修改接口")
    public Result updateProblemState(@RequestParam int problemId,@RequestParam @Validated({ValidGroup.State.class}) String state){
        return problemService.updateProblemState(problemId,state);
    }

    @DbLog(operation = "问题",type = LogType.UPDATE)
    @PutMapping("/updateproblemevaluate")
    @PreAuthorize("hasAuthority('problem_evaluate_update')")
    @ApiOperation(value = "售后问题评价接口")
    public Result updateProblemEvaluate(@RequestParam int id,@RequestParam
    @NotNull(message = "评价不能为空",groups = {ValidGroup.Evaluate.class})
    @Digits(integer=10,fraction=0,message="字段必须为整数")
    @Max(value = 10,message = "评价应为0-10的数字")
    @Min(value = 0,message = "评价应为0-10的数字") Integer evaluate){
        return problemService.updateProblemEvaluate(id,evaluate);
    }

    @DbLog(operation = "问题",type = LogType.DELETE)
    @DeleteMapping("/deleteproblem")
    @PreAuthorize("hasAuthority('problem_delete')")
    @ApiOperation(value = "任意售后问题删除接口")
    public Result deleteProblem(@RequestParam int problemId){
        return problemService.deleteProblem(problemId);
    }

}
